package com.yonyou.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.dao.WinningListDAO;
import com.yonyou.domain.RequestWhetherWinning;
import com.yonyou.domain.RequestWinningList;
import com.yonyou.domain.ResponseEntity;
import com.yonyou.domain.Winners;
import com.yonyou.domain.WinningList;
import com.yonyou.exception.BusinessException;
import com.yonyou.service.extend.UserServiceInterfaceClient;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = "/winninglist")
public class WinningListController{

	
	@Autowired
	private WinningListDAO winningListDAO;
	@Autowired
	public UserServiceInterfaceClient userServiceInterfaceClient;
	
	/**
	 * 保存中将人员名单
	 * @param winners
	 * @return
	 */
	@RequestMapping(value="/saveWinners",method = RequestMethod.POST)
	@ApiOperation(value = "保存中将人员名单",httpMethod ="POST", response = ResponseEntity.class,notes = "保存中将人员名单")
	public ResponseEntity<List<WinningList>> saveWinners(@ApiParam(required = true, name = "paramData", value = "中奖集合")@RequestBody Winners winners) {
		ResponseEntity<List<WinningList>> responseEntity = new ResponseEntity<List<WinningList>>();
		try {
			responseEntity.setData(winningListDAO.saveWinners(winners));
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (BusinessException e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg(e.getMessage());
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
			e.printStackTrace();
		}
		return responseEntity;
	}
	
	/**
	 * 判断是否中奖
	 * @param winners
	 * @return
	 */
	@RequestMapping(value="/whetherWinning",method = RequestMethod.POST)
	@ApiOperation(value = "查询会议下某个人员是否中奖",httpMethod ="POST", response = ResponseEntity.class,notes = "查询会议下某个人员是否中奖")
	public ResponseEntity<List<WinningList>> whetherWinning(@ApiParam(required = true, name = "paramData", value = "会议和用户参数信息")@RequestBody RequestWhetherWinning requestWhetherWinning) {
		ResponseEntity<List<WinningList>> responseEntity = new ResponseEntity<List<WinningList>>();
		try {
			responseEntity.setData(winningListDAO.whetherWinning(requestWhetherWinning.getMeeting_id(),requestWhetherWinning.getUser_id()));
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (BusinessException e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg(e.getMessage());
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
			e.printStackTrace();
		}
		return responseEntity;
	}
	/**
	 * 查询中奖人员名单
	 * @param meeting_id
	 * @param prize_id
	 * @return
	 */
	@RequestMapping(value="/list",method = RequestMethod.POST)
	@ApiOperation(value = "查询中将人员名单",httpMethod ="POST", response = ResponseEntity.class,notes = "查询中将人员名单")
	public ResponseEntity<List<WinningList>> list(@ApiParam(required = true, name = "paramData", value = "会议和奖项参数信息")@RequestBody RequestWinningList requestWinningList) {
		ResponseEntity<List<WinningList>> responseEntity = new ResponseEntity<List<WinningList>>();
		try {
			responseEntity.setData(winningListDAO.getWinningList(requestWinningList.getMeeting_id(),requestWinningList.getPrize_id()));
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;
	}
	
	/**
	 * 对中奖名单设为是否已经领奖
	 * @param meeting_id
	 * @param prize_id
	 * @return
	 */
	@RequestMapping(value="/update/{id}/{is_accept}",method = RequestMethod.POST)
	@ApiOperation(value = "对中奖名单设为是否已经领奖",httpMethod ="POST", response = ResponseEntity.class,notes = "对中奖名单设为是否已经领奖")
	public ResponseEntity<String> updateIsAccept(@ApiParam(required = true, name = "paramData", value = "中奖名单ID")@PathVariable String id,@ApiParam(required = true, name = "paramData", value = "是否已领奖")@PathVariable String is_accept) {
		ResponseEntity<String> responseEntity = new ResponseEntity<String>();
		winningListDAO.updateWinningList(id, is_accept);
		try {
			responseEntity.setData(id);
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;
	}
	/**
	 * 删除中将人员名单
	 * @param meeting_id
	 * @param prize_id
	 * @return
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	@ApiOperation(value = "删除全部中将人员名单",httpMethod ="POST", response = ResponseEntity.class,notes = "删除全部中将人员名单")
	public ResponseEntity<String> deleteWinninglist(@ApiParam(required = true, name = "paramData", value = "会议和奖项参数信息")@RequestBody RequestWinningList requestWinningList) {
		ResponseEntity<String> responseEntity = new ResponseEntity<String>();
		winningListDAO.deleteWinningList(requestWinningList.getMeeting_id(), requestWinningList.getPrize_id());
		try {
			responseEntity.setData(requestWinningList.getPrize_id());
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		} catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;
	}
	/**
	 * 抽奖并返回中将人员名单
	 * @param meeting_id
	 * @param prize_id
	 * @return
	 */
	@RequestMapping(value="/drawPrize/{prize_id}",method = RequestMethod.GET)
	@ApiOperation(value = "抽奖并返回中将人员名单",httpMethod ="GET", response = ResponseEntity.class,notes = "抽奖并返回中将人员名单")
	public ResponseEntity<List<WinningList>> drawPrize(@ApiParam(required = true, name = "paramData", value = "奖项ID") @PathVariable String prize_id) {
		ResponseEntity<List<WinningList>> responseEntity = new ResponseEntity<List<WinningList>>();
		try {
			responseEntity.setData(winningListDAO.drawPirze(prize_id));
			responseEntity.setCode(0);
			responseEntity.setErrmsg("");
		}catch (BusinessException e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg(e.getMessage());
		} 
		catch (Exception e) {
			responseEntity.setCode(1);
			responseEntity.setErrmsg("系统异常");
		}
		return responseEntity;
	}
}
